package com.qunchuang.timers;

import com.alibaba.druid.util.StringUtils;
import com.qunchuang.mapper.CronMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

@Configuration      //1.主要用于标记配置类，兼备Component的效果
@EnableScheduling   //2.开启定时任务
public class DynamicScheduleConfigurer implements SchedulingConfigurer {
    //注入mapper
    @Autowired
    @SuppressWarnings("all")
    private CronMapper cronMapper;

//    @Autowired
//    private LiftTaskStateTimers liftTaskStateTimers;
    @Autowired
    private LiftTaskStateTimersTcp liftTaskStateTimersTcp;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> {
                    try {
                        liftTaskStateTimersTcp.stateTimers();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                },
                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //2.1 从数据库获取执行周期
                    String cron = cronMapper.getCron();
                    //2.2 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    //2.3 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }
}
